package com.training.dao.impl.chenhao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.training.dao.chenhao.EmpDao;
import com.training.pojo.chengshu.MotorRoom;
import com.training.pojo.chengshu.UDept;
import com.training.pojo.chengshu.ULog;
import com.training.pojo.chengshu.UUser;
import com.training.pojo.chengshu.UserRole;

public class EmpdaoImpl extends HibernateDaoSupport implements EmpDao {

	public List<UUser> emplist(int start,int max) {
	    Session session=this.getSession(); 
	    String hql="from UUser where isdelete=1 and utype!=0"; 
	    Query query=session.createQuery(hql); 
	    query.setFirstResult(start-1); 
	    query.setMaxResults(5); 
	    return query.list(); 
		
		
	}
	public  int empsize() {
		HibernateTemplate h = this.getHibernateTemplate();
		String hql = "from UUser where isdelete=1 and utype!=0";
		List<UUser> list = h.find(hql);
        return list.size();
	}
	

	public List<UUser> serachemp(Integer utype, Integer deptid, String uname,
			Integer sex) {
		StringBuilder hql=new StringBuilder("");
		int a=0,b=0,c=0,d=0;
		if(utype!=0){
			a=1;
			//hql.append(" and utype="+utype+" ");
		}
		if(deptid!=0){
			b=1;
			//hql.append(" and UDept.deptid="+deptid+" ");
		}
		if(uname!=null && (!uname.equals(""))){
			c=1;
			//hql.append(" and uname like '%"+uname+"%' ");
		}
		if(sex!=2){
			d=1;
			//hql.append(" and sex="+sex+" ");
		}
		if(a==1||b==1||c==1||d==1){
			hql.append("from UUser where isdelete=1");
		    if(a==1) hql.append(" and utype="+utype+" ");
		    if(b==1)hql.append(" and UDept.deptid="+deptid+" ");
		    	if(c==1)hql.append(" and uname like '%"+uname+"%' ");
		    		if(d==1)hql.append(" and sex="+sex+" ");
		}else{
			hql.append("from UUser where isdelete=3");
		}
		
		System.out.println(hql.toString());
		Query query = this.getSession().createQuery(hql.toString());
		return query.list();
	}
		
	
	public List<UDept> deptname() {
	    HibernateTemplate h = this.getHibernateTemplate();
		String hql = "from UDept where isdelete=1";
		List<UDept> list = h.find(hql);
		
		/*for(int i=0;i<list.size();i++){
			System.out.println(list.get(i).getDeptname());
		}*/
		
        return list;
	}

	public void addemp(UUser emp, String udept) {
		HibernateTemplate h = this.getHibernateTemplate();
		emp.setIsdelete(1);
		int ude=Integer.parseInt(udept);
		UDept upt=(UDept)h.get(UDept.class, ude);
		emp.setUDept(upt);
		h.save(emp);
	}

	public UUser empdetail(String uid) {
		HibernateTemplate h = this.getHibernateTemplate();
		int id=Integer.parseInt(uid);
		UUser emp=h.get(UUser.class,id);
		return emp;
	}
	
	public List<UDept> chaDept(String uid) {
	    HibernateTemplate h = this.getHibernateTemplate();
		int id=Integer.parseInt(uid);
		String hql = "from UDept where UUser.uid = ?";
		List<UDept> list = h.find(hql,id);
	    return list;
	    
	}
	public List<ULog> chaLog(String uid) {
		HibernateTemplate h = this.getHibernateTemplate();
		int id=Integer.parseInt(uid);
		String hql = "from ULog where UUser.uid = ?";
		List<ULog> list = h.find(hql,id);
		return list;
	}
	public List<MotorRoom> chaMotor(String uid) {
		HibernateTemplate h = this.getHibernateTemplate();
		int id=Integer.parseInt(uid);
		String hql = "from MotorRoom where UUser.uid = ?";
		List<MotorRoom> list = h.find(hql,id);
		return list;
	}
	public List<UserRole> chaUserrole(String uid) {
		HibernateTemplate h = this.getHibernateTemplate();
		int id=Integer.parseInt(uid);
		String hql = "from UserRole where UUser.uid = ?";
		List<UserRole> list = h.find(hql,id);
		return list;
		}
	
	
	public void deleteemp(final String uid) {//涉及5个表，机房表，人员表，部门表，日志表，人员与功能对应表
		
		//事物管理人
		HibernateTransactionManager htm = new HibernateTransactionManager(this.getSessionFactory());
		//事物执行人
		
		TransactionTemplate tt = new TransactionTemplate(htm);
		tt.execute(new TransactionCallback<Object>() {
          public Object doInTransaction(TransactionStatus arg0) {
        	  HibernateTemplate h=getHibernateTemplate();
        	 //int id=Integer.parseInt(uid);
        	  List<UDept> uplist=chaDept(uid);
        	  List<ULog>  ullist=chaLog(uid);
        	  List<MotorRoom> mlist=chaMotor(uid);
        	  List<UserRole> urlist=chaUserrole(uid);
        	  if(uplist!=null){
        		//String hql="update UDept set UUser.uid=0 where UUser.uid=id";
        		//h.bulkUpdate(hql);
        		  for(UDept de:uplist){
        			/* UDept dept=new UDept();
        			 dept.setDeptid(de.getDeptid());
        			 dept.setDeptname(de.getDeptname());
        			 dept.setDeptnote(de.getDeptnote());
        			 dept.setIsdelete(de.getIsdelete());
        			 dept.setMotorRooms(de.getMotorRooms());
        			 h.delete(de);
        			 h.save(dept);*/
        			  de.setUUser(null);
        			  h.update(de);
        		  }
        	  }
        	  if(ullist!=null){
        		for(ULog lo:ullist){
        			lo.setUUser(null);
        			h.update(lo);
        		}
        	  }
        	  if(mlist!=null){
        		for(MotorRoom mo:mlist){
        			mo.setUUser(null);
        			h.update(mo);
        		}
        	  }
        	  if(urlist!=null){
        		for(UserRole ro:urlist){
        			h.delete(ro);
        			//ro.setUUser(null);
        			//h.update(ro);
        		}
        	  }
        	  int id=Integer.parseInt(uid);
        	  String sql="update UUser set isdelete=0 where uid="+id+"";
        	  h.bulkUpdate(sql);
				return null;
			}
			
		});
		
	}
	public Integer findDeptidByUserid(String userid){
		 HibernateTemplate h = this.getHibernateTemplate();
		  int ue=Integer.parseInt(userid);
		//String sql="select userid from UDept where deptid="+de+"";
		  UUser u=h.get(UUser.class, ue);
		 // System.out.println("finddept:"+u.getUDept().getDeptid());
		  return u.getUDept().getDeptid();
		 // return 0;
	}
	
	  public Integer  findUseridBydeptid(int deptid){
		  HibernateTemplate h = this.getHibernateTemplate();
		 // int de=Integer.parseInt(deptid);
		//String sql="select userid from UDept where deptid="+de+"";
		  UDept dept=h.get(UDept.class, deptid);
		  /*if(dept!=null){
		 // System.out.println("finduid:"+dept.getUUser().getUid());
		  
		  }*/
		  return dept.getUUser().getUid();
		 // return 0;
		  
	  }
	
	public void changeemp(final int id,final UUser emp,final String deptid) {
		HibernateTransactionManager htm = new HibernateTransactionManager(this.getSessionFactory());
		//事物执行人
		
		TransactionTemplate tt = new TransactionTemplate(htm);
		tt.execute(new TransactionCallback<Object>() {
          public Object doInTransaction(TransactionStatus arg0) {
        	  HibernateTemplate h=getHibernateTemplate();
        	  Integer a=findDeptidByUserid(id+"");
        	  Integer b=findUseridBydeptid(a);
        	  System.out.println("对或者错:"+(id==b));
        	  if(b==id){
        		  //如果原来部门表userid存在此人，则将之置空，然后再修改全局。
        		  List<UDept>list=chaDept(b+"");
        		  for(UDept de:list){
        			  de.setUUser(null);
        			  h.update(de);
        		  }
        	  }
        	  int dept=Integer.parseInt(deptid);
        	  UDept upt=(UDept)h.get(UDept.class,dept);
      	 	   emp.setUDept(upt);
      	 	    h.flush();//这两句话必须要！！！！
      	 	    h.clear();
      	 	    h.saveOrUpdate(emp);
			return null;
          }
		});
	}
	}
	
	

  
	


